package com.pifuke.patient.ui;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ScrollView;
import android.widget.TextView;
import cn.o.app.adapter.IItemView;
import cn.o.app.adapter.OAdapter;
import cn.o.app.adapter.OItemView;
import cn.o.app.net.INetTask;
import cn.o.app.net.NetTaskListener;
import cn.o.app.ui.FlowLayout;
import cn.o.app.ui.FlowLayout.OnLinesChangeListener;
import cn.o.app.ui.OAlert;
import cn.o.app.util.OUtil;

import com.pifuke.adapter.TreatmentProjectListAdapter;
import com.pifuke.patient.KepuXiangguanzhiliaoActivity;
import com.pifuke.patient.R;
import com.pifuke.patient.SymptomInfoActivity;
import com.smiier.skin.KepuHaoyaoshiActivity;
import com.smiier.skin.TreatmentDetailActivity;
import com.smiier.skin.extra.TreatmentProjectItemExtra;
import com.smiier.skin.net.DoctorTreatmentProjectListTask.DoctorTreatmentProjectItem;
import com.smiier.skin.net.KepuGetZhiliaoListTask;
import com.smiier.skin.net.KepuGetZhiliaoListTask.KepuGetZhiliaoListRequest;
import com.smiier.skin.net.KepuGetZhiliaoListTask.KepuGetZhiliaoListResponse;
import com.smiier.skin.ui.BasicStateView;
import com.smiier.skin.ui.ListViewForScrollView;
import com.smiier.skin.util.GlobalSettings;

/**
 * 疾病科普
 * 
 * @author Lennon
 * 
 */
public class SymptomScienceView extends BasicStateView implements
		OnItemClickListener {
	protected FlowLayout mDrugLayout;

	protected int mDrugPadding;

	private TextView breif;
	private TextView breif_more;
	private TextView drug_more;
	private TextView project_more;

	private static boolean isLoading;

	protected SparseBooleanArray mExpandPositions = new SparseBooleanArray();

	protected Drawable mArrowDown, mArrowUp;

	private FlowLayout drug_layout;

	private TreatmentProjectListAdapter mTreatmentAdapter;

	// private PullToRefreshListView mListTreatments;

	private ListViewForScrollView mListTreatments;

	protected int mColorBorderPurple;

	protected int mSymptomPaddingV;
	protected int mSymptomPaddingH;
	ScrollView mScrollParent;

	public SymptomScienceView(Context context) {
		super(context);
	}

	public SymptomScienceView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public SymptomScienceView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	public void onCreate() {
		super.onCreate();
		setContentView(R.layout.view_symptom_science);
		isLoading = false;
		init();
		mScrollParent = findViewById(R.id.scroll_parent, ScrollView.class);
		mDrugLayout = findViewById(R.id.drug_layout, FlowLayout.class);

		breif = findViewById(R.id.brief, TextView.class);
		breif_more = findViewById(R.id.bingli_brief_more, TextView.class);
		drug_more = findViewById(R.id.drug_more, TextView.class);
		project_more = findViewById(R.id.project_more, TextView.class);

		Resources mRes = getResources();
		mArrowDown = mRes.getDrawable(R.drawable.arrow_down);
		mArrowUp = mRes.getDrawable(R.drawable.arrow_up);

		mArrowDown.setBounds(0, 0, mArrowDown.getIntrinsicWidth(),
				mArrowDown.getIntrinsicHeight());
		mArrowUp.setBounds(0, 0, mArrowUp.getIntrinsicWidth(),
				mArrowUp.getIntrinsicHeight());

		drug_layout = findViewById(R.id.drug_layout, FlowLayout.class);

		mColorBorderPurple = mRes.getColor(R.color.border_purple);

		mDrugPadding = (int) OUtil.dp2px(getContext(), 8);

		mSymptomPaddingV = (int) OUtil.dp2px(getContext(), 6);
		mSymptomPaddingH = (int) OUtil.dp2px(getContext(), 15);

		mTreatmentAdapter = new TreatmentProjectListAdapter();
		mTreatmentAdapter.setShowDivider(false);
		/*
		 * mListTreatments = findViewById(R.id.list,
		 * PullToRefreshListView.class);
		 */

		mListTreatments = findViewById(R.id.normal_list,
				ListViewForScrollView.class);

		Log.i("testview", "mListTreatments.isnull:" + (mListTreatments == null));

		// mListTreatments.setPullRefreshEnabled(false);

		mListTreatments.setAdapter(mTreatmentAdapter);

		mListTreatments.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (mTreatmentAdapter != null) {
					DoctorTreatmentProjectItem item = (DoctorTreatmentProjectItem) mTreatmentAdapter
							.getDataProvider().get((int) id);
					Intent i = new Intent(getContext(),
							TreatmentDetailActivity.class);
					TreatmentProjectItemExtra extra = new TreatmentProjectItemExtra();
					extra.setUser(item);
					if (extra.putTo(i)) {
						startActivity(i);
					}
				}

			}
		});

		// loadData4Drug();
	}

	@Override
	public void onStart() {
		super.onStart();

		breif.setText(GlobalSettings.symptomItemInfo.Desc);

		breif_more.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(getContext(), SymptomInfoActivity.class);
				intent.putExtra("url", GlobalSettings.symptomItemInfo.Url);

				getContext().startActivity(intent);
			}
		});

		drug_more.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (drug_layout.getMaxLines() == 2) {
					drug_layout.setMaxLines(-1);
					drug_more.setText("收起");
					// mExpandPositions.put(mPosition, true);
					drug_more.setCompoundDrawables(null, null, mArrowUp, null);
				} else {
					drug_layout.setMaxLines(2);
					drug_more.setText("更多");
					// mExpandPositions.put(mPosition, false);
					drug_more
							.setCompoundDrawables(null, null, mArrowDown, null);
				}

			}
		});

		drug_layout.setMaxLines(2);

		drug_layout.setOnLinesChangeListener(new OnLinesChangeListener() {

			@Override
			public void onChanged(FlowLayout v, int lines) {
				if (v.getMaxLines() == -1) {
					if (lines == 2) {
						drug_more.setVisibility(View.INVISIBLE);
					} else {

						drug_more.setVisibility(View.VISIBLE);
						drug_more.setText("隐藏");
						drug_more.setCompoundDrawables(null, null, mArrowUp,
								null);
					}
				} else {
					if (lines == 2) {
						drug_more.setVisibility(View.INVISIBLE);
					} else {

						drug_more.setVisibility(View.VISIBLE);
						drug_more.setText("更多");
						drug_more.setCompoundDrawables(null, null, mArrowDown,
								null);

					}
				}
			}
		});

		Resources r = this.getContext().getResources();
		for (int i = 0; i < GlobalSettings.symptomItemInfo.Medicinal.size(); i++) {

			Drawable symptomDrawable = r
					.getDrawable(R.drawable.btn_filter_purple_selected);

			TextView symptomButton = new TextView(getContext());
			symptomButton.setTag(i);
			symptomButton.setTextColor(mColorBorderPurple);
			// symptomButton.setTextSize(mTextSize);
			symptomButton.setClickable(true);
			symptomButton.setBackgroundDrawable(symptomDrawable);
			symptomButton.setPadding(mSymptomPaddingH, mSymptomPaddingV,
					mSymptomPaddingH, mSymptomPaddingV);
			final String yaopin = GlobalSettings.symptomItemInfo.Medicinal
					.get(i).Name;

			symptomButton.setText(yaopin);

			symptomButton.setGravity(Gravity.CENTER_VERTICAL);

			symptomButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					int symptomPos = (Integer) v.getTag();

					// ParamsEncrypt.startWebHaoyaoshi(yaopin);

					Intent intent = new Intent();
					intent.putExtra("seekKey", yaopin);
					intent.setClass(getContext(), KepuHaoyaoshiActivity.class);
					getContext().startActivity(intent);

				}
			});

			drug_layout.addView(symptomButton);

		}

		loadTreatmentData();
	}

	private void loadTreatmentData() {

		if (!isLoading) {
			progress.setVisibility(View.VISIBLE);
			isLoading = true;
		}

		Log.i("testzhiliao", "loadTreatmentData");

		KepuGetZhiliaoListRequest req = new KepuGetZhiliaoListRequest();
		req.keywords = GlobalSettings.symptomItemInfo.Patient_Name;

		Log.i("testzhiliao", "req.keywords:"
				+ GlobalSettings.symptomItemInfo.Patient_Name.toString());
		KepuGetZhiliaoListTask task = new KepuGetZhiliaoListTask();
		task.setRequest(req);
		task.addListener(new NetTaskListener<KepuGetZhiliaoListTask.KepuGetZhiliaoListRequest, KepuGetZhiliaoListTask.KepuGetZhiliaoListResponse>() {
			@Override
			public void onComplete(
					INetTask<KepuGetZhiliaoListRequest, KepuGetZhiliaoListResponse> task,
					KepuGetZhiliaoListResponse response) {
				if (response != null && response.size() > 0) {

					if (response.size() > 2) {
						project_more.setVisibility(View.VISIBLE);
						setOnclickData(response);
						ArrayList<DoctorTreatmentProjectItem> data = new ArrayList<DoctorTreatmentProjectItem>();
						int size = response.size() > 5 ? 5 : response.size();
						for (int i = 0; i < size; i++) {
							data.add(response.get(i));
						}

						mTreatmentAdapter.setDataProvider(data);
						mTreatmentAdapter.notifyDataSetChanged();

					} else {
						mTreatmentAdapter.setDataProvider(response);
						mTreatmentAdapter.notifyDataSetChanged();
					}

					progress.setVisibility(View.GONE);
					// mScrollParent.fullScroll(View.FOCUS_UP);
					h.sendEmptyMessageDelayed(0, 0);

					// mListTreatments.setStackFromBottom(false);
				}

			}

			@Override
			public void onException(
					INetTask<KepuGetZhiliaoListRequest, KepuGetZhiliaoListResponse> task,
					Exception e) {
			}
		});
		add(task);

	}

	Handler h = new Handler() {
		public void handleMessage(android.os.Message msg) {
			// mScrollParent.scrollTo(0, 0);
			mScrollParent.smoothScrollTo(0, 0);
		};
	};

	protected void setOnclickData(final KepuGetZhiliaoListResponse response) {
		project_more.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(getContext(),
						KepuXiangguanzhiliaoActivity.class);

				Bundle bundle = new Bundle();
				bundle.putSerializable("data", response);
				intent.putExtras(bundle);

				startActivity(intent);
			}
		});

	}

	protected void loadData4Drug() {
		mDrugLayout.removeAllViews();
		Resources r = getContext().getResources();
		for (int i = 0, size = 3 + (int) (Math.random() * 5); i < size; i++) {
			TextView item = new TextView(getContext());
			item.setText("药品" + (i + 1));
			item.setTextColor(0xFFB33F77);
			item.setTextSize(16);
			item.setBackgroundDrawable(r
					.getDrawable(R.drawable.round_btn_white_with_stroke_pink));
			item.setPadding(mDrugPadding + mDrugPadding, mDrugPadding,
					mDrugPadding + mDrugPadding, mDrugPadding);
			item.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					OAlert alert = new OAlert(getContext());
					alert.setTitle(((TextView) v).getText());
					alert.show();
				}
			});
			mDrugLayout.addView(item);
		}
	}

	static class ProjectItem {

	}

	class ProjectAdapter extends OAdapter<ProjectItem> {

		@Override
		public IItemView<ProjectItem> getItemView() {
			return new ProjectItemView(getContext());
		}

	}

	class ProjectItemView extends OItemView<ProjectItem> {

		public ProjectItemView(Context context) {
			super(context);
		}

		@Override
		public void onCreate() {
			this.setContentView(R.layout.item_project);
		}

		@Override
		public void onResume() {

		}

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		isLoading = false;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		DoctorTreatmentProjectItem item = (DoctorTreatmentProjectItem) mTreatmentAdapter
				.getDataProvider().get((int) id);
		Intent i = new Intent(getContext(), TreatmentDetailActivity.class);
		TreatmentProjectItemExtra extra = new TreatmentProjectItemExtra();
		extra.setUser(item);
		if (extra.putTo(i)) {
			startActivity(i);
		}

	}

	/*
	 * @Override public void onResume() { super.onResume();
	 * MobclickAgent.onResume(getContext()); }
	 * 
	 * @Override public void onPause() { super.onPause();
	 * MobclickAgent.onPause(getContext()); }
	 */

}
